﻿/************************************************************************************
* Copyright (c) 2019 All Rights Reserved.
*命名空间：Commonality.Model
*文件名： XmlOperater
*创建人： Liebeskind
*创建时间：2019/6/17 16:04:10
*描述
*=====================================================================
*修改标记
*修改时间：2019/6/17 16:04:10
*修改人：Liebeskind
*描述：
************************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;

namespace Commonality.Model.Xml
{
    public class XmlOperater
    {
        public static void CreateNewXmlFile(string xmlFileName, string rootName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);//
            xmlDoc.AppendChild(dec);
            XmlElement root = xmlDoc.CreateElement(rootName);
            xmlDoc.AppendChild(root);
            if (!File.Exists(xmlFileName))
            {
                File.Create(xmlFileName).Close();
            }
            xmlDoc.Save(xmlFileName);
        }

        //************写XML文件****************************************************
        public static void WriteItemToXml(string xmlFileName, string rootName, string ElementName, string Value)
        {
            try
            {

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlElement node = (XmlElement)root.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                if (node != null && Value != "")
                {
                    node.InnerText = Value;
                }
                else if (node != null && Value == "")
                {
                    root.RemoveChild(root.SelectSingleNode(ElementName));
                }
                else if (node == null && Value != "")
                {
                    XmlElement child = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                    child.InnerText = Value;
                    root.AppendChild(child);//添加到根节点中 
                }
                xmlDoc.Save(xmlFileName); //保存其更改
            }
            catch (Exception)
            {

            }
        }

        private static readonly object writeXmlLock = new object();
        public static void WriteItemToXml(string xmlFileName, string rootName, string subrootName, string ElementName, string Value)
        {
            lock (writeXmlLock)
            {
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(xmlFileName); //加载xml文件
                    XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                    XmlNode subroot = root.SelectSingleNode(subrootName);
                    if (subroot != null)
                    {
                        XmlElement node = (XmlElement)subroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                        if (node != null && Value != "")
                        {
                            node.InnerText = Value;
                        }
                        else if (node != null && Value == "")
                        {
                            subroot.RemoveChild(subroot.SelectSingleNode(ElementName));
                        }
                        else if (node == null && Value != "")
                        {
                            XmlElement child = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                            child.InnerText = Value;
                            subroot.AppendChild(child);//添加到根节点中 
                        }
                        xmlDoc.Save(xmlFileName); //保存其更改
                    }
                    else
                    {
                        subroot = xmlDoc.CreateElement(subrootName);//创建一个子节点 
                        if (Value != "")
                        {
                            XmlElement child = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                            child.InnerText = Value;
                            subroot.AppendChild(child);//添加到根节点中 
                        }
                        root.AppendChild(subroot);//添加到根节点中 
                        xmlDoc.Save(xmlFileName); //保存其更改
                    }
                }
                catch (Exception)
                {

                }
            }

        }

        public static void WriteItemToXml(string xmlFileName, string rootName, string subrootName, string subsubrootName, string ElementName, string Value)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot = root.SelectSingleNode(subrootName);
                if (subroot == null)
                {
                    subroot = xmlDoc.CreateElement(subrootName);//创建一个子节点 
                    XmlNode subsubroot = xmlDoc.CreateElement(subsubrootName);//创建一个子子节点 
                    if (Value != "")
                    {
                        XmlElement child = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                        child.InnerText = Value;
                        subsubroot.AppendChild(child);//添加到根节点中 
                    }
                    subroot.AppendChild(subsubroot);
                    root.AppendChild(subroot);//添加到根节点中 
                    xmlDoc.Save(xmlFileName); //保存其更改
                }
                else
                {
                    XmlNode subsubroot = subroot.SelectSingleNode(subsubrootName);
                    if (subsubroot == null)
                    {
                        subsubroot = xmlDoc.CreateElement(subsubrootName);//创建一个子子节点 
                        if (Value != "")
                        {
                            XmlElement child = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                            child.InnerText = Value;
                            subsubroot.AppendChild(child);//添加到根节点中 
                        }
                        subroot.AppendChild(subsubroot);
                        root.AppendChild(subroot);
                        xmlDoc.Save(xmlFileName); //保存其更改
                    }
                    else
                    {
                        XmlNode child = subsubroot.SelectSingleNode(ElementName);
                        if (child == null)
                        {
                            child = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                        }

                        child.InnerText = Value;
                        subsubroot.AppendChild(child);//添加到根节点中 
                        subroot.AppendChild(subsubroot);
                        root.AppendChild(subroot);
                        xmlDoc.Save(xmlFileName); //保存其更改
                    }
                }
            }
#pragma warning disable CS0168 // 声明了变量“e”，但从未使用过
            catch (Exception e)
#pragma warning restore CS0168 // 声明了变量“e”，但从未使用过
            {
                //System.Windows.Forms.MessageBox.Show(e.ToString());
            }
        }

        public static void WriteItemToXml(string xmlFileName, string rootName, string subrootName, string subsubrootName, string subsubsubrootName, string ElementName, string Value)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot = root.SelectSingleNode(subrootName);
                if (subroot == null)
                {
                    subroot = xmlDoc.CreateElement(subrootName);//创建一个子节点 
                    XmlNode subsubroot = xmlDoc.CreateElement(subsubrootName);//创建一个子子节点 
                    if (Value != "")
                    {
                        XmlElement child = xmlDoc.CreateElement(subsubsubrootName);//创建一个子节点 
                        child.InnerText = Value;
                        subsubroot.AppendChild(child);//添加到根节点中 
                    }
                    subroot.AppendChild(subsubroot);
                    root.AppendChild(subroot);//添加到根节点中 
                    xmlDoc.Save(xmlFileName); //保存其更改
                }
                else
                {
                    XmlNode subsubroot = subroot.SelectSingleNode(subsubrootName);
                    if (subsubroot == null)
                    {
                        subsubroot = xmlDoc.CreateElement(subsubrootName);//创建一个子子节点 
                        if (Value != "")
                        {
                            XmlElement child = xmlDoc.CreateElement(subsubsubrootName);//创建一个子节点 
                            child.InnerText = Value;
                            subsubroot.AppendChild(child);//添加到根节点中 
                        }
                        subroot.AppendChild(subsubroot);
                        root.AppendChild(subroot);
                        xmlDoc.Save(xmlFileName); //保存其更改
                    }
                    else
                    {
                        XmlNode subsubsubroot = subsubroot.SelectSingleNode(subsubsubrootName);

                        if (subsubsubroot == null)
                        {
                            subsubsubroot = xmlDoc.CreateElement(subsubsubrootName);//创建一个子子节点 
                            if (Value != "")
                            {
                                XmlElement child = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                                child.InnerText = Value;
                                subsubsubroot.AppendChild(child);//添加到根节点中 
                            }
                            subsubroot.AppendChild(subsubsubroot);
                            subroot.AppendChild(subsubroot);
                            root.AppendChild(subroot);
                            xmlDoc.Save(xmlFileName); //保存其更改
                        }
                        else
                        {
                            XmlElement node = (XmlElement)subsubsubroot.SelectSingleNode(ElementName);

                            if (node == null)
                            {
                                node = xmlDoc.CreateElement(ElementName);//创建一个子节点 
                            }

                            node.InnerText = Value;

                            subsubsubroot.AppendChild(node);//添加到根节点中 
                            subsubroot.AppendChild(subsubsubroot);
                            subroot.AppendChild(subsubroot);
                            root.AppendChild(subroot);
                            xmlDoc.Save(xmlFileName); //保存其更改
                        }


                    }
                }
            }
#pragma warning disable CS0168 // 声明了变量“e”，但从未使用过
            catch (Exception e)
#pragma warning restore CS0168 // 声明了变量“e”，但从未使用过
            {
                //System.Windows.Forms.MessageBox.Show(e.ToString());
            }
        }


        public static void WriteArrayToXml(string xmlFileName, string rootName, string ChildElementName, string[] ChildAttributeName, string[] ChildAttributeValue, string[] GrandchildAttributeName, string[] GrandchildInnerText)
        {
            try
            {

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlElement child = xmlDoc.CreateElement(ChildElementName);//创建一个子节点 
                if (ChildAttributeName.Length > 0 && ChildAttributeName.Length == ChildAttributeValue.Length)//设置子节点属性
                {
                    for (int i = 0; i < ChildAttributeName.Length; i++)
                    {
                        child.SetAttribute(ChildAttributeName[i], ChildAttributeValue[i]);//设置该节点ISBN属性 
                    }
                }
                if (GrandchildAttributeName.Length > 0 && GrandchildAttributeName.Length == GrandchildInnerText.Length)//添加孙节点、设置其节点文本值
                {
                    for (int j = 0; j < GrandchildAttributeName.Length; j++)
                    {
                        XmlElement grandchild = xmlDoc.CreateElement(GrandchildAttributeName[j]);
                        grandchild.InnerText = GrandchildInnerText[j];//设置孙节点的文本值 
                        child.AppendChild(grandchild);//添加到子节点中 
                    }
                }
                root.AppendChild(child);//添加到﹤bookshop﹥节点中 
                xmlDoc.Save(xmlFileName); //保存其更改
            }
            catch (Exception)
            {

            }
        }

        public static void WriteArrayToXml(string xmlFileName, string rootName, string subrootName, string ChildElementName, string[] ChildAttributeName, string[] ChildAttributeValue, string[] GrandchildAttributeName, string[] GrandchildInnerText)
        {
            try
            {

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot;
                if (root.SelectSingleNode(subrootName) == null)
                {
                    subroot = xmlDoc.CreateElement(subrootName);
                    root.AppendChild(subroot);
                }
                else
                {
                    subroot = root.SelectSingleNode(subrootName);//查找根节点下一级节点
                }
                XmlElement child;
                if (subroot.SelectSingleNode(ChildElementName) == null)
                {
                    child = xmlDoc.CreateElement(ChildElementName);//创建一个子节点
                    subroot.AppendChild(child);//添加到﹤bookshop﹥节点中 
                }
                else
                {
                    child = (XmlElement)subroot.SelectSingleNode(ChildElementName);
                }

                if (ChildAttributeName.Length > 0 && ChildAttributeName.Length == ChildAttributeValue.Length)//设置子节点属性
                {
                    for (int i = 0; i < ChildAttributeName.Length; i++)
                    {
                        child.SetAttribute(ChildAttributeName[i], ChildAttributeValue[i]);//设置该节点ISBN属性 
                    }
                }
                if (GrandchildAttributeName.Length > 0 && GrandchildAttributeName.Length == GrandchildInnerText.Length)//添加孙节点、设置其节点文本值
                {
                    for (int j = 0; j < GrandchildAttributeName.Length; j++)
                    {
                        XmlElement grandchild = xmlDoc.CreateElement(GrandchildAttributeName[j]);
                        grandchild.InnerText = GrandchildInnerText[j];//设置孙节点的文本值 
                        child.AppendChild(grandchild);//添加到子节点中 
                    }
                }

                xmlDoc.Save(xmlFileName); //保存其更改  
            }
            catch (Exception)
            {

            }
        }

        public static void WriteArrayToXml(string xmlFileName, string rootName, string subrootName, string subsubrootName, string ChildElementName, string[] ChildAttributeName, string[] ChildAttributeValue, string[] GrandchildAttributeName, string[] GrandchildInnerText)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot = root.SelectSingleNode(subrootName);//查找根节点下一级节点
                XmlElement subsubroot = xmlDoc.CreateElement(subsubrootName);//创建一个子子节点 
                XmlElement child = xmlDoc.CreateElement(ChildElementName);//创建一个子节点 
                if (ChildAttributeName.Length > 0 && ChildAttributeName.Length == ChildAttributeValue.Length)//设置子节点属性
                {
                    for (int i = 0; i < ChildAttributeName.Length; i++)
                    {
                        child.SetAttribute(ChildAttributeName[i], ChildAttributeValue[i]);//设置该节点ISBN属性 
                    }
                }
                if (GrandchildAttributeName.Length > 0 && GrandchildAttributeName.Length == GrandchildInnerText.Length)//添加孙节点、设置其节点文本值
                {
                    for (int j = 0; j < GrandchildAttributeName.Length; j++)
                    {
                        XmlElement grandchild = xmlDoc.CreateElement(GrandchildAttributeName[j]);
                        grandchild.InnerText = GrandchildInnerText[j];//设置孙节点的文本值 
                        child.AppendChild(grandchild);//添加到子节点中 
                    }
                }
                subsubroot.AppendChild(child);
                subroot.AppendChild(subsubroot);
                xmlDoc.Save(xmlFileName); //保存其更改  
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 将数组写入到xml中
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <param name="rootName"></param>
        /// <param name="ElementName"></param>
        /// <param name="Value"></param>
        public static bool WriteArrayToXml(string xmlFileName, string rootName, string[] ElementName, string[] Value)
        {
            try
            {
                if (ElementName.Count() != Value.Count())
                {
                    return false;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                for (int i = 0; i < ElementName.Count(); i++)
                {
                    XmlElement node = (XmlElement)root.SelectSingleNode(ElementName[i]);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                    if (node != null && Value[i] != "")
                    {
                        node.InnerText = Value[i];
                    }
                    else if (node != null && Value[i] == "")
                    {
                        root.RemoveChild(root.SelectSingleNode(ElementName[i]));
                    }
                    else if (node == null && Value[i] != "")
                    {
                        XmlElement child = xmlDoc.CreateElement(ElementName[i]);//创建一个子节点 
                        child.InnerText = Value[i];
                        root.AppendChild(child);//添加到根节点中 
                    }
                }

                xmlDoc.Save(xmlFileName); //保存其更改
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static void WriteArrayToXml(string xmlFileName, string rootName, string subrootName, string[] ElementName, string[] Value)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot = root.SelectSingleNode(subrootName);
                if (subroot != null)
                {
                    for (int i = 0; i < ElementName.Count(); i++)
                    {
                        XmlElement node = (XmlElement)subroot.SelectSingleNode(ElementName[i]);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                        if (node != null && Value[i] != "")
                        {
                            node.InnerText = Value[i];
                        }
                        else if (node != null && Value[i] == "")
                        {
                            subroot.RemoveChild(subroot.SelectSingleNode(ElementName[i]));
                        }
                        else if (node == null && Value[i] != "")
                        {
                            XmlElement child = xmlDoc.CreateElement(ElementName[i]);//创建一个子节点 
                            child.InnerText = Value[i];
                            subroot.AppendChild(child);//添加到根节点中 
                        }
                        xmlDoc.Save(xmlFileName); //保存其更改
                    }


                }
                else
                {
                    for (int i = 0; i < ElementName.Count(); i++)
                    {
                        subroot = xmlDoc.CreateElement(subrootName);//创建一个子节点 
                        if (Value[i] != "")
                        {
                            XmlElement child = xmlDoc.CreateElement(ElementName[i]);//创建一个子节点 
                            child.InnerText = Value[i];
                            subroot.AppendChild(child);//添加到根节点中 
                        }
                        root.AppendChild(subroot);//添加到根节点中 
                        xmlDoc.Save(xmlFileName); //保存其更改
                    }


                }
            }
            catch (Exception)
            {

            }
        }

        //************读XML文件****************************************************
        public static string ReadItemFromXml(string xmlFileName, string rootName, string ElementName)
        {
            try
            {


                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlElement node = (XmlElement)root.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                if (node != null)
                {
                    return node.InnerText;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {

                return "";
            }
        }

        public static string ReadItemFromXml(string xmlFileName, string rootName, string subrootName, string ElementName)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root;
                XmlNode subroot;
                XmlElement node;
                if (xmlDoc.SelectSingleNode(rootName) == null)
                {
                    return "";
                }
                else
                {
                    root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                }
                if (root.SelectSingleNode(subrootName) == null)
                {
                    return "";
                }
                else
                {
                    subroot = root.SelectSingleNode(subrootName);
                }
                if (subroot.SelectSingleNode(ElementName) == null)
                {
                    return "";
                }
                else
                {
                    node = (XmlElement)subroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                }
                if (node != null)
                {
                    return node.InnerText;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }


        public static string ReadItemFromXml(string xmlFileName, string rootName, string subrootName, string subsubrootName, string ElementName)
        {
            try
            {

                //if (xmlFileName != lsFileName)
                //{
                //    xmlDoc = new XmlDocument();
                //    xmlDoc.Load(xmlFileName); //加载xml文件
                //}
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件

                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot = root.SelectSingleNode(subrootName);

                if (subroot != null)
                {
                    XmlNode subsubroot = subroot.SelectSingleNode(subsubrootName);
                    XmlElement node = (XmlElement)subsubroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null

                    //lsFileName = xmlFileName;

                    if (node != null)
                    {
                        return node.InnerText;
                    }
                    else
                    {
                        return "";
                    }
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }
        public static string ReadItemFromXml(string xmlFileName, string rootName, string subrootName, string subsubrootName, string subsubsubrootName, string ElementName)
        {
            try
            {

                //if (xmlFileName != lsFileName)
                //{
                //    xmlDoc = new XmlDocument();
                //    xmlDoc.Load(xmlFileName); //加载xml文件
                //}
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件

                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot = root.SelectSingleNode(subrootName);
                XmlNode subsubroot = subroot.SelectSingleNode(subsubrootName);

                if (subsubroot != null)
                {
                    XmlNode subsubsubroot = subsubroot.SelectSingleNode(subsubsubrootName);
                    XmlElement node = (XmlElement)subsubsubroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null

                    //lsFileName = xmlFileName;

                    if (node != null)
                    {
                        return node.InnerText;
                    }
                    else
                    {
                        return "";
                    }
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }
        public static string ReadItemFromXml(XmlNode root, string subrootName, string ElementName)
        {
            try
            {
                XmlNode subroot = root.SelectSingleNode(subrootName);
                XmlElement node = (XmlElement)subroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                if (node != null)
                {
                    return node.InnerText;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string ReadItemFromXml(XmlNode root, string subrootName, string subsubrootName, string ElementName)
        {
            try
            {
                XmlNode subroot = root.SelectSingleNode(subrootName);
                XmlNode subsubroot = subroot.SelectSingleNode(subsubrootName);
                XmlElement node = (XmlElement)subsubroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                if (node != null)
                {
                    return node.InnerText;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static void ReadArrayFromXml(string xmlFileName, string rootName, string ChildElementName, string[] ChildAttributeName, out string[] ChildAttributeValue, out string[] GrandchildInnerText)
        {

            string[] array_tempChildAttributeValue = new string[ChildAttributeName.Length];
            List<string> tempGrandchildInnerText = new List<string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFileName); //加载xml文件
            XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
            XmlElement node = (XmlElement)root.SelectSingleNode(ChildElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
            for (int i = 0; i < ChildAttributeName.Length; i++)
            {
                array_tempChildAttributeValue[i] = node.GetAttribute(ChildAttributeName[i]);
            }
            XmlNodeList grandchildNode = node.ChildNodes;
            foreach (XmlNode TempGrandchild in grandchildNode)
            {
                tempGrandchildInnerText.Add(TempGrandchild.InnerText);
            }
            string[] array_tempGrandchildInnerText = new string[tempGrandchildInnerText.Count()];
            for (int j = 0; j < tempGrandchildInnerText.Count(); j++)
            {
                array_tempGrandchildInnerText[j] = tempGrandchildInnerText[j];
            }
            ChildAttributeValue = array_tempChildAttributeValue;
            GrandchildInnerText = array_tempGrandchildInnerText;

        }

        public static void ReadArrayFromXml(string xmlFileName, string rootName, string subrootName, string ChildElementName, string[] ChildAttributeName, out string[] ChildAttributeValue, out string[] GrandchildInnerText)
        {


            string[] array_tempChildAttributeValue = new string[ChildAttributeName.Length];
            List<string> tempGrandchildInnerText = new List<string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFileName); //加载xml文件
            XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
            XmlNode subroot = root.SelectSingleNode(subrootName);//查找根节点下一级节点
            XmlElement node = (XmlElement)subroot.SelectSingleNode(ChildElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
            for (int i = 0; i < ChildAttributeName.Length; i++)
            {
                array_tempChildAttributeValue[i] = node.GetAttribute(ChildAttributeName[i]);
            }
            XmlNodeList grandchildNode = node.ChildNodes;
            foreach (XmlNode TempGrandchild in grandchildNode)
            {
                tempGrandchildInnerText.Add(TempGrandchild.InnerText);
            }
            string[] array_tempGrandchildInnerText = new string[tempGrandchildInnerText.Count()];
            for (int j = 0; j < tempGrandchildInnerText.Count(); j++)
            {
                array_tempGrandchildInnerText[j] = tempGrandchildInnerText[j];
            }
            ChildAttributeValue = array_tempChildAttributeValue;
            GrandchildInnerText = array_tempGrandchildInnerText;

        }

        public static void ReadArrayFromXml(string xmlFileName, string rootName, string subrootName, string subsubrootName, string ChildElementName, string[] ChildAttributeName, out string[] ChildAttributeValue, out string[] GrandchildInnerText)
        {


            string[] array_tempChildAttributeValue = new string[ChildAttributeName.Length];
            List<string> tempGrandchildInnerText = new List<string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFileName); //加载xml文件
            XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
            XmlNode subroot = root.SelectSingleNode(subrootName);//查找根节点下一级节点
            XmlNode subsubroot = subroot.SelectSingleNode(subsubrootName);
            XmlElement node = (XmlElement)subsubroot.SelectSingleNode(ChildElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
            for (int i = 0; i < ChildAttributeName.Length; i++)
            {
                array_tempChildAttributeValue[i] = node.GetAttribute(ChildAttributeName[i]);
            }
            XmlNodeList grandchildNode = node.ChildNodes;
            foreach (XmlNode TempGrandchild in grandchildNode)
            {
                tempGrandchildInnerText.Add(TempGrandchild.InnerText);
            }
            string[] array_tempGrandchildInnerText = new string[tempGrandchildInnerText.Count()];
            for (int j = 0; j < tempGrandchildInnerText.Count(); j++)
            {
                array_tempGrandchildInnerText[j] = tempGrandchildInnerText[j];
            }
            ChildAttributeValue = array_tempChildAttributeValue;
            GrandchildInnerText = array_tempGrandchildInnerText;

        }

        public static void ReadArrayFromXml(XmlNode subroot, string rootName, string ChildElementName, string[] ChildAttributeName, out string[] ChildAttributeValue, out string[] GrandchildInnerText)
        {

            string[] array_tempChildAttributeValue = new string[ChildAttributeName.Length];
            List<string> tempGrandchildInnerText = new List<string>();
            XmlNode root = subroot.SelectSingleNode(rootName);//查找根节点
            XmlElement node = (XmlElement)root.SelectSingleNode(ChildElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
            for (int i = 0; i < ChildAttributeName.Length; i++)
            {
                array_tempChildAttributeValue[i] = node.GetAttribute(ChildAttributeName[i]);
            }
            XmlNodeList grandchildNode = node.ChildNodes;
            foreach (XmlNode TempGrandchild in grandchildNode)
            {
                tempGrandchildInnerText.Add(TempGrandchild.InnerText);
            }
            string[] array_tempGrandchildInnerText = new string[tempGrandchildInnerText.Count()];
            for (int j = 0; j < tempGrandchildInnerText.Count(); j++)
            {
                array_tempGrandchildInnerText[j] = tempGrandchildInnerText[j];
            }
            ChildAttributeValue = array_tempChildAttributeValue;
            GrandchildInnerText = array_tempGrandchildInnerText;

        }

        //=====================================删除xml文件=============================================/
        public static bool RemoveItemFromXml(string xmlFileName, string rootName, string ElementName)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlElement node = (XmlElement)root.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null

                if (node != null)
                {
                    node.RemoveAll();
                    //xmlDoc.RemoveChild(node);
                    xmlDoc.Save(xmlFileName); //保存其更改
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool RemoveItemFromXml(string xmlFileName, string rootName, string subrootName, string ElementName)
        {
            try
            {

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root;
                XmlNode subroot;
                XmlElement node;
                if (xmlDoc.SelectSingleNode(rootName) == null)
                {
                    return false;
                }
                else
                {
                    root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                }
                if (root.SelectSingleNode(subrootName) == null)
                {
                    return false;
                }
                else
                {
                    subroot = root.SelectSingleNode(subrootName);
                }
                if (subroot.SelectSingleNode(ElementName) == null)
                {
                    return false;
                }
                else
                {
                    node = (XmlElement)subroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                }

                if (node != null)
                {
                    node.RemoveAll();
                    xmlDoc.Save(xmlFileName); //保存其更改
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool RemoveItemFromXml(string xmlFileName, string rootName, string subrootName, string subsubrootName, string ElementName)
        {
            try
            {

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFileName); //加载xml文件
                XmlNode root = xmlDoc.SelectSingleNode(rootName);//查找根节点
                XmlNode subroot = root.SelectSingleNode(subrootName);
                if (subroot != null)
                {
                    XmlNode subsubroot = subroot.SelectSingleNode(subsubrootName);
                    XmlElement node = (XmlElement)subsubroot.SelectSingleNode(ElementName);//找到的第一个匹配节点，如果没有匹配的节点就返回 null
                    if (node != null)
                    {
                        node.RemoveAll();
                        xmlDoc.Save(xmlFileName); //保存其更改
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }

            }
            catch (Exception)
            {
                return false;
            }
        }


        //=================================================================================//
        /// <summary>
        /// 根据类型名称加载对应单条信息
        /// </summary>
        /// <param name="xmlFile">xml文件</param>
        /// <param name="rootName">根节点</param>
        /// <param name="subNode">子节点1</param>
        /// <param name="subsubNode">子节点2</param>
        /// <param name="Name">类型名称</param>
        /// <returns>返回查找到的单条信息</returns>
        public static string LoadXmlListByName(string xmlFile, string rootName, string subNode, string subsubNode, string elementName, string Name)
        {
            try
            {
                string rtnStr = "";
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFile);
                XmlNode root = xmlDoc.SelectSingleNode(rootName);
                XmlNode sub = root.SelectSingleNode(subNode);
                XmlNodeList lists = sub.SelectNodes(subsubNode);

                foreach (XmlNode xn in lists)
                {
                    XmlElement xe = (XmlElement)xn;
                    XmlNode node = xe.SelectSingleNode(elementName);
                    if (node != null)
                    {
                        if (node.InnerText == Name)
                        {
                            rtnStr = xe.LastChild.InnerText;
                        }
                    }
                }
                return rtnStr;
            }
            catch (Exception)
            {
                return "";
            }
        }

        private static XmlNodeList FindAll(string xmlFile, string rootName, string subNode, string subsubNode)
        {
            XmlNodeList rtnList = null;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            XmlNode root = xmlDoc.SelectSingleNode(rootName);
            XmlNode sub = root.SelectSingleNode(subNode);
            rtnList = sub.SelectNodes(subsubNode);
            return rtnList;
        }

        public static string FindResultByName(string xmlFile, string rootName, string subNode, string subsubNode, string elementName, string Name)
        {
            string rtnName = "";
            XmlNodeList list = null;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            XmlNode root = xmlDoc.SelectSingleNode(rootName);
            XmlNode sub = root.SelectSingleNode(subNode);
            list = sub.SelectNodes(subsubNode);

            foreach (XmlNode xn in list)
            {
                //xn.SelectSingleNode();
                XmlNodeList childList = xn.ChildNodes;
                foreach (XmlNode child in childList)
                {

                }
            }
            return rtnName;
        }

        /// <summary>
        /// 加载指定条件下的所有节点信息
        /// </summary>
        /// <param name="xmlFile"></param>
        /// <param name="rootName"></param>
        /// <param name="subNode"></param>
        /// <param name="subsubNode"></param>
        /// <param name="elementName"></param>
        /// <param name="rtnList"></param>
        public static void LoadXmlList(string xmlFile, string rootName, string subNode, string subsubNode, string elementName, out List<string> rtnList)
        {
            rtnList = new List<string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            XmlNode root = xmlDoc.SelectSingleNode(rootName);
            XmlNode sub = root.SelectSingleNode(subNode);

            XmlNodeList lists = sub.SelectNodes(subsubNode);
            foreach (XmlNode xn in lists)
            {
                XmlElement xe = (XmlElement)xn;
                XmlNode node = xe.SelectSingleNode(elementName);
                if (node != null)
                {
                    rtnList.Add(node.InnerText);
                }
            }
        }

        public static void UpdateXmlByName(string xmlFile, string rootName, string subNode, string subsubNode, string elementName, string oldName, string newName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            XmlNode root = xmlDoc.SelectSingleNode(rootName);
            XmlNode sub = root.SelectSingleNode(subNode);

            XmlNodeList lists = sub.SelectNodes(subsubNode);

            foreach (XmlNode xn in lists)
            {
                XmlElement xe = (XmlElement)xn;
                XmlNode node = xe.SelectSingleNode(elementName);
                if (node != null)
                {
                    if (node.InnerText == oldName)
                    {
                        xe.LastChild.InnerText = newName;
                        xmlDoc.Save(xmlFile); //保存其更改
                    }
                }

            }

        }


    }
}
